package com.seu.dm.controllers;

import com.seu.dm.annotations.permissions.BuyerPermission;
import com.seu.dm.dto.UserBaseDTO;
import com.seu.dm.entities.Favorite;
import com.seu.dm.services.FavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Greeting on 2017/3/14.
 */
@Controller
public class FavoriteController {
    @Autowired
    private FavoriteService favoriteService;


    /**
     * 判断当前用户所浏览的商品是否已添加到收藏夹
     * @param productId
     * @param request
     * @return
     */
    @RequestMapping(value = "isFavorite/{productId}")
    @BuyerPermission
    public String judgeIsFavorite(@PathVariable Integer productId, HttpServletRequest request, Model model){
        HttpSession httpSession = request.getSession();
        UserBaseDTO userBaseDTO = (UserBaseDTO) httpSession.getAttribute("userBase");
        Integer buyerId = userBaseDTO.getId();

        List<Favorite> favorites = favoriteService.findFavoritesByBuyerId(buyerId);
        for (Favorite favoriteIter : favorites) {
            if(favoriteIter.getProductId()== productId)
                model.addAttribute("favorite",favoriteIter);
        }
        return "buyer/product_detail";
    }

    /**
     * 返回当前用户的收藏夹商品列表
     * @param request
     * @return
     */
    @RequestMapping(value = "getFavorites")
    @BuyerPermission
    public String getFavorites(HttpServletRequest request,Model model){
        HttpSession httpSession = request.getSession();
        UserBaseDTO userBaseDTO = (UserBaseDTO) httpSession.getAttribute("userBase");
        Integer buyerId=userBaseDTO.getId();
        List<Favorite> favorites =favoriteService.findFavoritesByBuyerId(buyerId);

        model.addAttribute("favorites",favorites);

        return "buyer/buyer_favorite";
    }

    /**
     * 添加商品到收藏夹
     */
    @RequestMapping(value = "addToFavorite/{productId}")
    @BuyerPermission
    public String addToFavorite(@PathVariable Integer productId, HttpServletRequest request,Model model){
        HttpSession httpSession = request.getSession();
        UserBaseDTO userBaseDTO = (UserBaseDTO) httpSession.getAttribute("userBase");
        if(userBaseDTO == null){
            model.addAttribute("message","请登录");
            model.addAttribute("jumpUrl",request.getRequestURL());
            return "common/alert";
        }
        Integer buyerId = userBaseDTO.getId();

        Favorite favorite=new Favorite();
        favorite.setBuyerId(buyerId);
        favorite.setProductId(productId);
        favoriteService.addNewFavorite(favorite);

        String url = request.getRequestURL().toString();
        model.addAttribute("message","收藏成功!");
        model.addAttribute("jumpUrl",url);
        return "common/alert";

    }

}
